/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Foobar. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.core.service.impl;

import static com.google.gwt.thirdparty.guava.common.base.Preconditions.checkArgument;

import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import com.smouquet.ecovoiturage.core.bean.TrajetMidiItem;
import com.smouquet.ecovoiturage.core.dao.TrajetDAO;
import com.smouquet.ecovoiturage.core.domain.Trajet;
import com.smouquet.ecovoiturage.core.domain.TrajetUtilisateur;
import com.smouquet.ecovoiturage.core.service.TrajetService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author sebastien.mouquet
 */
public class TrajetServiceImpl implements TrajetService
{
    @Inject
    private TrajetDAO dao;

    
    @Override
    public List<TrajetMidiItem> getAllTrajetsMidiByJour(Date jour)
    {
        if(jour == null) {
            jour = new Date();
        }
        
        List<Trajet> trajets = dao.findByJourOrderByLieuArriveeHeureDepart(jour);
        
        List<TrajetMidiItem> list = new ArrayList<TrajetMidiItem>();
        
        TrajetMidiItem item;
        for(Trajet trajet : trajets)
        {
            item = new TrajetMidiItem();
            item.setIdTrajet(trajet.getId());
            item.setLieuArrivee(trajet.getLieuArrivee().getNom());
            item.setHeureDepart(trajet.getHeureDepart());
            item.setHeureRetour(trajet.getHeureRetour());
            
            if(trajet.getInteresses() != null)
            {
                item.setNbInteresses(trajet.getInteresses().size());
                item.setNbPlaces(0);
                for(TrajetUtilisateur tu : trajet.getInteresses()) {
                    item.setNbPlaces(item.getNbPlaces()+tu.getNbPlaces().intValue());
                }
            }
            else
            {
                item.setNbInteresses(0);
                item.setNbPlaces(0);
            }
            
            list.add(item);
        }
        
        return list;
    }

    @Override
    public Trajet getById(Integer idTrajet)
    {
        checkArgument(idTrajet != null, "idTrajet is required");
        
        return dao.getReference(idTrajet);
    }

    @Override
    @Transactional
    public Trajet addTrajet(Trajet trajet)
    {
        checkArgument(trajet != null, "trajet is required");

        return dao.merge(trajet);
    }

    @Override
    @Transactional
    public Trajet updateTrajet(Trajet trajet)
    {
        checkArgument(trajet != null, "trajet is required");
        
        return dao.merge(trajet);
    }
    
    
}
